package com.type.TypingDetector;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class DBAdapterTest extends ListActivity {
	private ArrayList<String> queryString;
	//private ArrayList<DummyDataObject> queryDDO;
	private DatabaseQuery query;
	private boolean[] toShow;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> colNames= new ArrayList<String>(Arrays.asList("Title", "XDom", "XAvg", "XMax", "XMin", "YDom", "YAvg", "YMax", "YMin", "ZDom", "ZAvg", "ZMax", "ZMin"));
        ArrayList<String> colOpts= new ArrayList<String>(Arrays.asList("text","real","real","real","real", "real","real","real","real", "real","real","real","real"));
        String[] rows = {"ID", "Title", "Dominant", "Average"};
        String[] rowOpts = {"integer", "text", "real", "real"};
        String[] titles = {"Phone Detached", "Idle", "Walking", "Running", "Jumping"};
        double[] domFreqs = {10.324, 8.987, 7.658, 50.8, 46.210};
        double[] avgAccel = {9.8, 10.1, 12.3, 33.8, 28.5};
        
       /* for (int i=0; i<rows.length; i++) {
        	dbKeys.add(rows[i]);
        	dbOpts.add(rowOpts[i]);
        }*/
        
        query = new DatabaseQuery(this, colNames, colOpts, "AccelVals");
        
        // Populate the database
        for (int i=0; i<titles.length; i++) {
        	query.appendData("Title", titles[i]);
        	query.appendData("XDom", "" + domFreqs[i]);
        	query.appendData("XAvg", "" + avgAccel[i]);
        	query.appendData("XDom", "" + domFreqs[i]);
        	query.appendData("XMax", "" + avgAccel[i]);
        	query.appendData("XMin", "" + domFreqs[i]);
        	query.appendData("YAvg", "" + avgAccel[i]);
        	query.appendData("YDom", "" + domFreqs[i]);
        	query.appendData("YMax", "" + avgAccel[i]);
        	query.appendData("YMin", "" + domFreqs[i]);
        	query.appendData("ZAvg", "" + avgAccel[i]);
        	query.appendData("ZDom", "" + domFreqs[i]);
        	query.appendData("ZMax", "" + avgAccel[i]);
        	query.appendData("ZMin", "" + domFreqs[i]);
        	query.addRow();
        }
        //query.appendData("Dominant", "" + 1);
        //query.appendData("ID", "" + 1);
    	//query.appendData("Title", titles[2]);
    	
    	//query.appendData("Average Frequency", "" + avgAccel[0]);
        //query.addRow();
        //query.appendData("Title", "Row One");
        //query.addRow();
        //query.appendData("Title", "Row Two");
        //query.addRow();

        // Pull the data from the database
        //queryString = query.getData(new String[] {"ID", "Title", "Dominant", "Average"}, null, null, null, null, null);
        queryString = new ArrayList<String>();
        /*queryDDO = query.getDataD(new String[] {"ID", "Title", "Dominant", "Average"}, null, null, null, null, null);
        for (int i=0; i<queryDDO.size(); i++) {
        	queryString.add(queryDDO.get(i).getId() + "-" + queryDDO.get(i).getTitle() + ":"
        			+ queryDDO.get(i).getDomFreq() + "/" + queryDDO.get(i).getAvgAcc());
        }
        
        toShow=new boolean[queryDDO.size()];
        for (int i=0; i<toShow.length;i++) {
        	toShow[i] = false;
        }*/
        
        try {
			query.destroy();
		} catch (Throwable e) {
			e.printStackTrace();
		}

        // Set the ListView
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, queryString));
        ListView lv = getListView(); 
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id) {
			      // When clicked, show a toast with the TextView text
		    	toShow[position]=!toShow[position];
		    	Toast.makeText(getApplicationContext(), "" + toShow[position],
			          Toast.LENGTH_SHORT).show();
			    }
			  });
    }
}

